package com.etc.team2.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.team2.bean.Station;
import com.etc.team2.service.StationService;

public class AddStationServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * 接收到addstation.jsp传递的数据，产生Station对象
		 * 调用StationService中的addStation方法
		 * 返回true，跳转到AllStationServlet页面
		 * false的页面等春哥做。。。先打印出结果调试就行
		 * */
		String referer=request.getHeader("referer");
		if (referer==null||!referer.startsWith("http://localhost:")) {
			response.sendRedirect("home.jsp");
			return;
		}
        request.setCharacterEncoding("utf-8");
        String stationname=request.getParameter("name");
        String address=request.getParameter("address");
        Station station=new Station(stationname, address);
        boolean result= new StationService().addStation(station);
        if (result) {
        	response.sendRedirect("AllStationServlet");
		} else {
			response.sendRedirect("error.jsp");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}
